package com.wyp168.leetcode;

/**
 * @ClassName SearchInsert
 * @Description TODO
 * @Author wyp168
 * @Date 2022/8/4 09:34
 */
public class SearchInsert {
    public static void main(String[] args) {
        int[] nums = {1,2,4,5,6,7};
        int target = 3;
        SolutionSearchInsert solutionSearchInsert = new SolutionSearchInsert();
        System.out.println(solutionSearchInsert.searchInsert(nums, target));
    }
}

class SolutionSearchInsert {
    public int searchInsert(int[] nums, int target) {
        int length = nums.length;
        int left = 0, right = length -1;

        while (left <= right) {
            int mid = (left + right) / 2;

            if (nums[mid] > target) {
                right = mid - 1;
            }
            if (nums[mid] < target) {
                left = mid + 1;
            }
            if (nums[mid] == target) {
                return mid;
            }
        }
        return left;
    }

}
